package com.mmw.ruiji.controller;


import com.mmw.ruiji.entity.Category;
import com.mmw.ruiji.entity.R;
import com.mmw.ruiji.server.CategoryServer;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryServer categoryServer;

    @GetMapping("/page")
    public R<List<Category>> getCategories(){
        return R.success(categoryServer.getCategories());
    }

    @PostMapping("/add")
    public R<String> addCategory(String name){
        int i = categoryServer.addCategory(name);
        if(i==1)
            return R.success("添加分类成功");
        return R.error("添加分类失败");
    }

    @PostMapping("/delete")
    public R<String> deleteCategory(Long id){
        System.out.println(id);
        int i = categoryServer.deleteCategory(id);
        if (i == 1){
            return R.success("删除分类成功");
        }
        return R.success("删除分类失败");
    }
}
